﻿@{
    ViewBag.Title = "门店分组";
}

@section scripts
{  
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script> 
}
   <div class="mini-splitter" style="width:100%;height:100%;">
    <div size="270" showcollapseButton="true">
        @Html.Partial("~/Views/Modular/ToolbarType.cshtml")
        <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
            <table>
                <tr>
                    <td width ="80px" align="right">编码或名称: </td>
                    <td width ="110px" ><input class="mini-textbox" id ="FSearchKey" width ="110px" name = "FSearchKey"  onkeypress="setPress()"/></td>
                    <td width ="80px" align="center">
                        <a class="mini-button" iconCls="icon-search" onclick="LoadAgentList" plain="true">查询</a>
                    </td>
                </tr>
            </table>
        </div>
        <div class="mini-fit">
            <div id="datagrid2" class="mini-datagrid" style="width:100%;height:100%;" pageSize="15"
                borderStyle="border:1;" url="GetGoodsPriceBook"
                allowCellSelect="true" allowCellEdit="true"showColumnsMenu="true"fitColumns="false"  
                onrowdblclick="editClass"  sortMode="client" onselectionchanged="LoadListData" selectOnLoad="true">
                <div property="columns">
                    <div type="checkcolumn" ></div>
                    <div type="indexcolumn" width="40" headerAlign="center" >序号</div>
                    <div field="FNumber" name="FNumber" width="70" headeralign="center" allowsort="true">方案编码</div>
                    <div field="FName" name="FName" width="110" headeralign="center" allowsort="true">方案名称</div>
                </div>
            </div>
        </div>
    </div>
       
    <div showCollapseButton="true">
        <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <a class="mini-button" id="fresh" name="fresh" iconCls="icon-search" onclick="search" plain="true">刷新</a>
                        <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="newBill" plain="true" visible="@ViewBag.putin">加入商品</a>
                        <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="saveas" plain="true" visible="@ViewBag.save">保存</a>
                        <a class="mini-button" id="delrow" name="delrow" iconCls="icon-remove" onclick="removeRows" plain="true" visible="@ViewBag.putout">移除商品</a>                       
                         <span class="separator"></span>
                        <a class="mini-button" id="close" name="close" iconCls="icon-close" onclick="CloseWindowEx" plain="true">关闭</a>
                    </td>
                </tr>
            </table>       
        </div>
        <table>
            <tr>
                <td width ="120px" align="right">条码、编码或名称: </td>
                <td width ="150px" ><input class="mini-textbox" width ="150px" id ="FSearchKeyS" name = "FSearchKeyS" onkeypress="setkeyPress()"/></td>
                <td width ="100px" align="right">
                    <div id="chkSelectAll" name="chkSelectAll"  class="mini-checkbox" style ="margin-right:2px" readOnly="false" value ="false" text="显示所有方案"></div>
                </td>
                <td width ="100px" align="center">
                    <a class="mini-button" iconCls="icon-search" onclick="search(0)" plain="true">查询</a>
                </td>
            </tr>
        </table>
        <div class="mini-fit" >
            <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;" pageSize="15"
                    borderStyle="border:1;" url="../Price/GetGoodsBookDetail" oncellcommitedit="onCellCommitEdit"
                    allowCellSelect="true" allowCellEdit="true" showColumnsMenu="true" fitColumns="false" skipReadOnlyCell="true" 
                    multiSelect="true" editNextOnEnterKey="true"  editNextRowCell="false" allowCellEdit="true" navEditMode="true">
                <div property="columns">
                    <div type="checkcolumn" ></div>
                    <div type="indexcolumn" width="40" headerAlign="center" >序号</div>
                    <div field="FCode" name="FCode" width="130" headeralign="center" allowsort="true">商品条码</div>
                    <div field="FName" name="FName" width="190" headeralign="center" allowsort="true">商品名称</div>
                    <div field="FSpec" name="FSpec" width="90" headeralign="center" allowsort="true">规格型号</div>
                    <div field="FUnitName" name="FUnitName" width="40" headeralign="center" allowsort="true">单位</div>
                    <div field="FRmoPrice" name="FRmoPrice" width="90" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">配送价
                        <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0" />
                    </div>
                    <div field="FSelPrice" name="FSelPrice" width="90" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">批发价
                        <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0" />
                    </div>
                    <div field="FPosPrice" name="FPosPrice" width="90" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">零售价
                        <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0" />
                    </div>
                    <div field="FVipPrice" name="FVipPrice" width="90" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">会员价
                        <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0" />
                    </div>
                    <div field="FConvert" name="FConvert" width="70" headeralign="center" allowsort="true">内装数</div>
                    <div field="FNumber" name="FNumber" width="100" headeralign="center" allowsort="true">商品编码</div>
                    <div field="FBookNumber" name="FBookNumber" width="100" headeralign="center" allowsort="true">价格方案编码</div>
                    <div field="FBookName" name="FBookName" width="150" headeralign="center" allowsort="true">价格方案名称</div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    mini.parse();
    var grid = mini.get("datagrid1");
    var grid2 = mini.get("datagrid2");
    var sModifyFlag = 0;
    function FormShown() {
        LoadPara();
        LoadAgentList();
        LoadListData(0);
    }
    function setkeyPress() {
        LoadListData(0);
    }
    function setPress() {
        LoadAgentList();
    }
    function LoadListTree() {
        LoadAgentList();
    }
    function LoadAgentList() {
        var json = "[{'FSystemAppID':'"+parent.window.getSystemAppID()+"',";
        json = json + "'FNumber':" + "'" + mini.getbyName("FSearchKey").getValue() + "'";
        json = json + "}]";
        grid2.load({ mjson: json });
    }

    function LoadPara() {
        $.ajax({
            url: 'GetGoodsPricePara',
            data: { mSystemAppID: parent.window.getSystemAppID() },
            dataType: 'text',
            async: false,
            success: function (text) {
                var dataR = mini.decode(text);
                if (dataR.length > 0) {
                    var mdata = dataR[0];
                    sModifyFlag = mdata.FModifyFlag;
                    if (mdata.FModifyFlag != 1) {
                        mini.get("addClass").setEnabled(false);
                        mini.get("editClass").setEnabled(false);
                        mini.get("removeClass").setEnabled(false);
                        mini.get("chkSelectAll").setEnabled(false);                        
                    }
                }
            }
        });
    }


    function setGridColumn(bKind) {
        switch (bKind) {
            case 0:
                grid.updateColumn("FRmoPrice", { visible: true });
                grid.updateColumn("FSelPrice", { visible: true });
                grid.updateColumn("FPosPrice", { visible: true });
                grid.updateColumn("FVipPrice", { visible: true });
                break;
            case 1:
                grid.updateColumn("FRmoPrice", { visible: true });
                grid.updateColumn("FSelPrice", { visible: true });
                grid.updateColumn("FPosPrice", { visible: false });
                grid.updateColumn("FVipPrice", { visible: false });
                break;
            case 2:
                grid.updateColumn("FRmoPrice", { visible: false });
                grid.updateColumn("FSelPrice", { visible: false });
                grid.updateColumn("FPosPrice", { visible: true });
                grid.updateColumn("FVipPrice", { visible: true });
                break;
        }
    }


    //加载资料列表
    function LoadListData(mdatatype) {
        var sSystemAppID = parent.window.getSystemAppID();
        var node = grid2.getSelected()
        if (node) {
            var mBookPriceID = node.FItemID;
            var sBookType = node.FBookType;
            if (mini.getbyName("chkSelectAll").getChecked() == true) {
                mBookPriceID = "-1";
                sBookType = 0;
            }
            setGridColumn(sBookType);
            var json = "[{";
            json = json + "'FSystemAppID':" + "'" + sSystemAppID + "',";
            json = json + "'mBookPriceID':" + "'" + mBookPriceID + "',";
            json = json + "'mNumber':" + "'" + mini.getbyName("FSearchKeyS").getValue() + "',";
            json = json + "'mGoodsID':" + "'0',";
            json = json + "'mDatatype':" + "'" + mdatatype + "'";
            json = json + "}]";
            grid.load({ mydata: json });
            mini.get("save").setEnabled(false);
            mExportDataUrl = grid.url;
            mExportDataJson = json;//mydata:mExportDataJson
        }
    }

    function saveas() {
        var rows = grid.getChanges();
        var json = mini.encode(rows);
        $.post("SaveGoodsPriceData", { json: json, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                grid.reload();
                mini.get("save").setEnabled(false);
                mini.showTips({ content: '保存成功', state: "success", x: "center", y: "top", timeout: 3000 });
            }
            else {
                mini.showTips({ content: '保存失败 ' + ret.error, state: "danger", x: "center", y: "top", timeout: 3000 });
            }
        });
    }

    //搜索资料列表
    function search(mdatatype) {
        LoadListData(mdatatype);
    }

    ///价格方案
    function addClass() {
        var node = grid2.getSelected();        
        mini.open({
            url: "../Price/GoodsPriceInfo",
            title: "价格方案", width: 500, height: 330,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new"};
                iframe.contentWindow.SetInfoData(data);
            },
            ondestroy: function (action) {
                grid2.reload();
            }
        });
    }

    //修改分类
    function editClass() {
        if (sModifyFlag != 1) return;
        var node = grid2.getSelected();
        var rCnt = grid.getData().length;
        if (node) {
            if (node.FItemID < 0)
                return;
            mini.open({
                url: "../Price/GoodsPriceInfo",
                title: "价格方案", width: 500, height: 330,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { action: "edit", FItemID: node.FItemID, FGoodsCount: rCnt };
                    iframe.contentWindow.SetInfoData(data);
                },
                ondestroy: function (action) {
                    grid2.reload();
                }
            });
        }
    }

    //删除分类
    function removeClass() {
        if (grid.getData().length > 0) {
            mini.alert("该方案有商品资料存在，不能删除！ ");
            return;
        }
        var node = grid2.getSelected()
        var FSystemAppID = parent.window.getSystemAppID();
        if (node.FItemID > 0) {
            mini.confirm("确定要删除选中的记录吗？", "提示",
                function (action) {
                    if (action == "ok") {
                        var json = "{'FItemID':'" + node.FItemID + "'}";
                        $.post("../Price/delBook", { json: json, mSystemAppID: FSystemAppID }, function (data) {
                            var ret = mini.decode(data);
                            if (ret.status == 1) {
                                grid2.reload();
                                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                            }
                            else {
                                mini.alert("异常:" + ret.error);
                            }
                        });
                    }
                });
        } else {
            mini.alert("请选中一条记录");
        }
    }



    function newBill() {
        var uID = parent.window.getSystemAppID();
        var node = grid2.getSelected();
        if (!node)
        {
            mini.alert("请选择一个方案再新增");
            return;
        }
        if (node.FItemID == 0) {
            mini.alert("请选择一个方案再新增");
            return;
        }
        var sPriceBookID = node.FItemID;
        mini.open({
            url: "../Bill/GetBillGoods",
            title: "选择商品", width: 900, height: 500,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { key: "", mBillType: "99", mStockID: sPriceBookID, FObjectID: sPriceBookID };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action) {
                    for (var i = 0, l = action.length; i < l; i++) {
                        var rw = action[i];
                        var hFItemID = rw.FItemID;
                        var mFItemID = "";
                        for (var j = 0, m = grid.getData().length; j < m; j++) {
                            var rs = grid.getRow(j);
                            if (hFItemID == rs.FGoodsID) {
                                mFItemID = rs.FGoodsID;
                            }
                        }
                        if (mFItemID == "") {
                            var newRow = {
                                FPriceBookID: sPriceBookID, FGoodsID: rw.FItemID, FNumber: rw.FNumber, FName: rw.FName, FSpec: rw.FSpec,
                                FUnitName: rw.FUnitName, FCode: rw.FCode, FSelPrice: rw.FSelPrice, FRmoPrice: rw.FRmoPrice,
                                FConvert: rw.FConvert, FPosPrice: rw.FPosPrice, FVipPrice: rw.FVipPrice
                            };
                            grid.addRow(newRow, grid.getData().length);
                            mini.get("save").setEnabled(true);
                        }
                    }
                }
            }
        });
    }

    function onCellCommitEdit(e) {
        mini.get("save").setEnabled(true);//编辑后保存按钮可用
    }

    //删除资料
    function removeRows() {
        var rows = grid.getSelecteds();
        if (rows.length > 0) {
            grid.removeRows(rows, true);
            mini.get("save").setEnabled(true);
        }
    }

    function CloseWindowEx(action) {
        var rCnt = grid.getData().length;
        if (mini.get("save").getEnabled() && (rCnt > 0)) {
            mini.confirm("有输入的数据未保存，确定要关闭吗？", "提问",
            function (action) {
                if (action == "ok") {
                    CloseWindow();
                }
            });
        } else {
            CloseWindow();
        }
    }

</script>